﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using WindowsLive;

namespace goMatic
{
    public partial class Start : System.Web.UI.Page
    {
        const string LoginCookie = "webauthtoken";
        internal static dbEntities oDB;
        
        // Initialize the WindowsLiveLogin module.
        static WindowsLiveLogin wll = new WindowsLiveLogin(true);

        protected static string AppId = wll.AppId;

        protected string CustomerId;
        protected Customers oCostomer;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HttpRequest req = HttpContext.Current.Request;
                HttpCookie loginCookie = req.Cookies[LoginCookie];
                oDB = new dbEntities();
                
                try
                {
                    CustomerId = ViewState["CustomerID"].ToString();
                }
                catch { }
                if (string.IsNullOrEmpty(CustomerId))
                {
                    if (loginCookie != null)
                    {
                        string token = loginCookie.Value;

                        if (!string.IsNullOrEmpty(token))
                        {
                            WindowsLiveLogin.User user = wll.ProcessToken(token);

                            if (user != null)
                            {
                                CustomerId = oDB.Customers.First(t => t.liveid == user.Id).customerid;
                                ViewState.Add("CustomerID", CustomerId);
                            }
                        }
                    }
                    else
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }

                try
                {
                    Label1.Text = oDB.Customers.First(t => t.customerid == CustomerId).firstname;
                    CCU.ccucmdSoapClient oCCU = new CCU.ccucmdSoapClient();
                    oCCU.Open();
                    oCCU.NewSessionIDAsyncAsync(CustomerId);

                    //oCCU.Close();
                }
                catch { }

                //Get Rooms and Functions
                if (!string.IsNullOrEmpty(CustomerId))
                {
                    DropDownList1.DataSource = oDB.Rooms.Where(t => t.CustomerId == CustomerId).ToList();
                    DropDownList1.DataTextField = "Name";
                    DropDownList1.DataValueField = "id";
                    DropDownList1.DataBind();

                    DropDownList2.DataSource = oDB.Functions.Where(t => t.CustomerId == CustomerId).ToList();
                    DropDownList2.DataTextField = "Name";
                    DropDownList2.DataValueField = "id";
                    DropDownList2.DataBind();
                }
            }
            else
            {
                try
                {
                    CustomerId = ViewState["CustomerID"].ToString();
                    if (string.IsNullOrEmpty(CustomerId))
                    {
                        Response.Redirect("~/Default.aspx");
                    }
                }
                catch { Response.Redirect("~/Default.aspx"); }
            }
        }

        protected void btConfigHM_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/ConfigHM.aspx");
        }

        protected void btInit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/XMLRPCInit.aspx");
        }

        protected void btShowData_Click(object sender, EventArgs e)
        {
            string sURL = "~/DeviceData.aspx?write=true";
            if (DropDownList1.Text != "%")
            {
                sURL = sURL + "&room=" + DropDownList1.Text;
            }
            if (DropDownList2.Text != "%")
            {
                sURL = sURL + "&function=" + DropDownList2.Text;
            }

            Response.Redirect(sURL);

        }

    }
}